/*
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-15 09:26:34
 * @LastEditors: charles
 * @LastEditTime: 2021-11-15 11:35:22
 */
import {createStore,combineReducers,applyMiddleware} from 'redux'
import _ from 'lodash'
import saga from './saga'
import createSagaMiddleware from 'redux-saga'
// state
// action
// reducer
const state_todos = {list:[{id:1,name:"去看电影"}],loading:false};
function todos(state=state_todos,action){
  switch (action.type) {
    case "ADD_TODO":
      return {
        ...state,
        list:state.list.concat(action.payload)
      }
    case "REMOVE_TODO":
      _.remove(state.list,item=>item.id === action.payload)
      return {
        ...state
      }
    default:
      return state;
  }
}

function user(state={},action){

  return state;
}

// 资讯
function articles(state={list:[],loading:false},action){
  switch (action.type) {
    case "REFRESH_ARTICLES":
      return {
        ...state,
        list:action.payload
      }
    case "ALTER_LOADING":
      return {
        ...state,
        loading:action.payload
      }
    default:
      return state;
  }
  
}
// 1. 创建saga中间件
const sagaMiddleware =  createSagaMiddleware()
const store = createStore(
  combineReducers({todos,user,articles}),
  applyMiddleware(sagaMiddleware) // 2. 在状态机中应用中间件
)
sagaMiddleware.run(saga)  // 3. 运行中间件
export default store;
